﻿using Newtonsoft.Json;
using RTW.OA.Areas.Common.Servers;
using RTW.OA.Attributes;
using RTW.OA.Controllers;
using RTW.OA.Models;
using System;
using System.Web.Mvc;

namespace RTW.OA.Areas.Document.Controllers
{
    [CheckLogin(IsChildPage = true)]
    public class BorrowController : BaseController
    {
        private DocumentServers documentServers;
        private DocumentBorrowServers documentBorrowServers;

        public BorrowController()
        {
            documentServers = new DocumentServers();
            documentBorrowServers = new DocumentBorrowServers();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult MyBorrow()
        {
            return View();
        }

        public string GetDataTableJsonResult()
        {
            var datalist = documentServers.GetBorrowList();
            foreach (var item in datalist)
            {
                item.official_document_borrows = documentBorrowServers.GetData(item.DOC_ID);
            }
            return JsonConvert.SerializeObject(datalist);
        }

        public string GetMyBorrowDataTableJsonResult()
        {
            var datalist = documentBorrowServers.GetDataByUserNO(this.CurrentUser.USER_NO);
            return JsonConvert.SerializeObject(datalist);
        }

        public ActionResult Borrow(int id)
        {
            //var model = documentServers.GetModelById(id);
            official_document_borrow model = new official_document_borrow
            {
                DOC_ID = id
            };
            return View(model);
        }

        public ActionResult ReBorrow(int id)
        {
            var model = documentBorrowServers.GetModelById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Borrow(official_document_borrow model)
        {
            model.STATUS = 0;
            if (model.BORROW_ID != 0)
            {
                documentBorrowServers.Update(model);
            }
            else
            {
                model.CREATE_TIME = DateTime.Now;
                model.CREATE_USER_NO = (Session["UserInfo"] as user).USER_NO;
                documentBorrowServers.Add(model);
            }
            return View();
        }

        [HttpGet]
        public ActionResult Approval(int id)
        {
            var model = documentBorrowServers.GetModelById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult SetApproval(int id, int status,string remark=null)
        {
            try
            {
                var model = new official_document_borrow
                {
                    BORROW_ID = id,
                    STATUS = status,
                    APPROVAL_NO = this.CurrentUser.USER_NO,
                    APPROVAL_NAME = this.CurrentUser.TRUE_NAME,
                    APPROVAL_REMARK = remark
                };
                documentBorrowServers.UpdateStatus(model);
                return Json(new
                {
                    code = 100,
                    msg = "更新成功"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new
                {
                    code = 0,
                    msg = "更新失败，原因：" + e.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}